$(document).ready(function()
{
	ChiliBook.recipeFolder     = baseUrl+'/js/chili/';
	ChiliBook.stylesheetFolder = baseUrl+'/js/chili/';
	ChiliBook.lineNumbers      = true;
	//$('pre').chili();
	

	$('#editBtn').click(function()
	{
		$('#editTitle').slideToggle('slow');
	});
	
	$('#moveBtn').click(function()
	{
		$('#moveThread').slideToggle('slow');
	});
	
	$('.delMsg').click(function() {
		if(confirm(langDelConfirm))
		{
			var form = $('#modAction');
			form.attr('action', $(this).attr('href'));
			form.submit();
		}
		return false;
	});
	
	$('.modAction').click(function() {
		var form = $('#modAction');
		form.attr('action', $(this).attr('href'));
		form.submit();

		return false;
	});
	

	// forward to post
	if(forwardId)
	{
		var offset = $('a.postJumper[postId='+forwardId+']').offset();
		window.scrollTo(0, offset.top - 100);
	}
	
	var editorInit = false;
	
	// editor events
	$('.addReply').click(function()
	{
		if(!editorInit)
		{
			initEditor();
			editorInit = true;
		}
		$('#replyBox').show();
		window.location = thisUrl+'#replylink';
		$('.addReply').hide();
	});
	
	$('.quoteMsg').click(function()
	{
		$('#replyBox').show();
		window.location = thisUrl+'#replylink';
		$('.addReply').hide();
		
		var item = $(this);
		var above = item.parent().parent().parent();
		var message = above.find('div.forumPost');
		var username = above.find('.username');
		
		var quote = '<em>'+langPostedBy+' '+username.html()+'</em><br><blockquote>'+message.html()+'</blockquote><p>&nbsp;</p><p>&nbsp;</p>';
		
		if(editorInit)
		{
			tinyMCE.getInstanceById('message').execCommand('mceInsertContent', false, quote);
		}
		else
		{
			$('#message').val($('#message').val()+quote);
			initEditor();
			editorInit = true;
		}
	});

	$('.editMsg').click(function()
	{
		if(!editorInit)
		{
			initEditor();
			editorInit = true;
		}
		var item = $(this);
		var above = item.parent().parent().parent();
		
		if(above.find('textarea').length != 0)
		{
			return;
		}
		
		var message = above.find('div.forumPost').hide();
		randId = 'edit'+Math.round(Math.random() * 1000000);

		message.after('<div><textarea style="width:100%; height:300px;" id="'+randId+'">'+message.html()+'</textarea>' +
			'<div style="display:block; margin:5px auto; width:10py; text-align:center; overflow:show;">' +
			'<input type="button" value="'+langBtnSubmit+'"> <input type="button" value="'+langBtnCancel+'"></div>' +
			'<img src="'+url+'/images/common/loading.gif" style="display:none"></div>');
		mapper = setInterval(mapEditor, 100);
	});
	
	function mapEditor()
	{
		var editor = $('#'+randId);
		var container = editor.parent();
		if(editor == undefined)
		{
			return;
		}
		else
		{
			clearInterval(mapper);
		}
		
		tinyMCE.execCommand('mceAddControl', false, randId);
		
		container.find('div input:last-child').click(function()
		{
			var postDiv = container.prevAll('div.forumPost');
			container.remove();
			postDiv.show();
		});
		
		container.find('div input:first-child').click(function()
		{
			var message = encodeURIComponent(tinyMCE.getInstanceById(randId).getContent());
			var jumperLink = container.parent().prev();
			var postId = jumperLink.attr('postId');
			var groupId = jumperLink.attr('groupId');
			var threadId = jumperLink.attr('threadId');
			var indicator = editor.nextAll('img');
			
			$.ajax(
			{
				type: 'POST',
				url: baseUrl+'/?page=forums&op=addpost&group_id='+groupId+'&thread_id='+threadId,
				data: 'send=true&post_id='+postId+'&message='+escape(message),
				beforeSend: function() {
					indicator.fadeIn('fast');
					tinyMCE.execCommand('mceRemoveControl', false, randId);
					editor.attr('disabled', 'disabled');
					editor.nextAll('input').attr('disabled', 'disabled');
				},
				success: function(msg) {
					indicator.fadeOut('fast');
					if(msg == 'ok')
					{
						var postDiv = container.prevAll('div.forumPost');
						container.remove();
						postDiv.html(decodeURIComponent(message)).show();
					}
				}
			});
		});
	}
	
	$('#showGallery').click(function()
	{
		$('#galleryForEditor').slideToggle('slow');
		$.get(url+'/?page=ajaxresponder&op=getgallery&foo='+Math.random(), function(data)
		{
			$('#galleryForEditor').html(data);
			setTimeout(mapGallery, 500);
		});
	});
	
	function mapGallery()
	{
		$('#galleryForEditor img').click(function()
		{
			var src = $(this).attr('src').replace('thumbnails/', '');
			var insert = '<img src="'+src+'" />';
			tinyMCE.getInstanceById('message').execCommand('mceInsertContent', false, insert);
		});
	}
	
	
	
});